<?php
namespace app\common\service;

use think\facade\Db;

class User extends Base {
    static $AUTH_CHECK = true; //默认必须登录
    public function info(){

        $where = [];
        $page_id = input('page_id');
        if($page_id){
            $where['id'] = $page_id;
        }else{
            $where['is_home'] = 2;
        }

        $pagedata = Db::name('designerpage')->where($where)->find();
        if(!$pagedata){
            return $this->json(['status'=>0,'msg'=>'页面不存在']);
        }
        $user_id = $this->user_id;
        $pagecontent = logic('PageLogic')->initpagecontent($pagedata['content'],$user_id);

        $pageinfo = json_decode($pagedata['pageinfo'],true);
        $pageparams = $pageinfo[0]['params'];
        $rdata = [];
        $rdata['pageinfo'] = $pageparams;
        $rdata['pagecontent'] = $pagecontent;
        $result = [
            'status' => true,
            'msg' => '',
            'data' => $rdata,
        ];
        return $result;
    }

    public function getinfo(){
        $user_id = $this->user_id;
        $userinfo = Db::name('users')->where('user_id',$user_id)->find();
        $userinfo['wximg'] = $userinfo['wximg']?explode(',',$userinfo['wximg']):'';
        $userinfo['aliimg'] = $userinfo['aliimg']?explode(',',$userinfo['aliimg']):'';
        $banklist =  ['工商银行', '农业银行', '中国银行', '建设银行', '招商银行', '邮储银行', '交通银行', '浦发银行', '民生银行', '兴业银行', '平安银行', '中信银行', '华夏银行', '广发银行', '光大银行', '北京银行', '宁波银行'];
        return ['status'=>true,'userinfo'=>$userinfo,'banklist'=>$banklist];
    }
    public function setUserInfo(){

        $user_id = $this->user_id;
        $user_info = Db::name('users')->where('user_id',$user_id)->field('username,nickname,head_pic')->find();
        $params = input();
        if ($_FILES['head_pic']['tmp_name']) {
            $file = $this->request->file('head_pic');
            $image_upload_limit_size = config('image_upload_limit_size');
            $validate = ['size'=>$image_upload_limit_size,'ext'=>'jpg,png,gif,jpeg'];
            $dir = UPLOAD_PATH.'head_pic/';
            if (!($_exists = file_exists($dir))){
                $isMk = mkdir($dir);
            }
            $parentDir = date('Ymd');
            $info = $file->validate($validate)->move($dir, true);
            if($info){
                $head_pic = '/'.$dir.$parentDir.'/'.$info->getFilename();
            }else{
                $this->error($file->getError());//上传错误提示错误信息
                $result = [
                    'status' => false,
                    'msg' => $file->getError(),
                    'data' => $user_info
                ];
                return $result;
            }
        }
        $data = [];
        //$data['head_pic'] = $head_pic?:'';
        //$data['truename'] = $params['truename'];
        $data['nickname'] = $params['truename'];
        $data['sex'] = $params['sex']?:0;//0男 1女
        $data['birthday'] = strtotime($params['birthday']);
        Db::name('users')->where('user_id',$user_id)->update($data);

        $result = [
            'status' => true,
            'msg' => '',
            'data' => ''
        ];
        return $result;
    }

    public function updateWxLogo(){
        $logo = input('logo');
        $nickname = input('nickname');
        $data = [];
        $data['head_pic'] = $logo?:'';
//        $data['nickname'] = $nickname;
        $user_id = $this->user_id;
        Db::name('users')->where('user_id',$user_id)->update($data);
        $result = [
            'status' => true,
            'msg' => '',
            'data' => ''
        ];
        return $result;
    }

    public function getuserinfo(){
        $domain_url = getCurUrl();
        $user_id = $this->user_id;
        $userinfo = Db::name('users')->where('user_id',$user_id)->find();
        $userinfo['head_pic'] = $userinfo['head_pic']?:$domain_url.'/static/img/touxiang.png';
        $haspwd = $userinfo['paypwd']?1:0;
        $rdata = [
            'userinfo' => $userinfo,
            'haspwd' => $haspwd
        ];
        $result = [
            'status' => true,
            'msg' => '',
            'data' => $rdata,
        ];
        return $result;
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     *
    ALTER TABLE `sy_users`
    CHANGE COLUMN `zhifubao` `aliaccount`  varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '支付宝账号' AFTER `weixin`,
    ADD COLUMN `weixinname`  varchar(255) NOT NULL DEFAULT '' COMMENT '微信昵称' AFTER `bank_name`,
    ADD COLUMN `wximg`  varchar(255) NOT NULL DEFAULT '' COMMENT '微信收款图片' AFTER `weixinname`,
    ADD COLUMN `aliaccountname`  varchar(255) NOT NULL DEFAULT '' COMMENT '支付宝姓名' AFTER `aliaccount`,
    ADD COLUMN `aliimg`  varchar(255) NOT NULL DEFAULT '' COMMENT '支付宝收款码' AFTER `aliaccountname`;
    ALTER TABLE `sy_users`
    ADD COLUMN `bank_address`  varchar(255) NOT NULL DEFAULT '' COMMENT '分行' AFTER `bank_name`;



     */
    public function set(){
        $params = input();
        $data = [];
        if(!empty($params['head_pic'])){
            $data['head_pic'] = $params['head_pic'];
        }
        if(!empty($params['nickname'])){
            $data['nickname'] = $params['nickname'];
        }
        if(!empty($params['sex'])){
            $data['sex'] = $params['sex'];
        }
        if(!empty($params['weixin'])){
            $data['weixin'] = $params['weixin'];
        }
        if(!empty($params['weixinname'])){
            $data['weixinname'] = $params['weixinname'];
        }
        if(!empty($params['wximg'])){
            $data['wximg'] = implode(',',$params['wximg']);
        }
        if(!empty($params['aliaccount'])){
            $data['aliaccount'] = $params['aliaccount'];
        }
        if(!empty($params['aliaccountname'])){
            $data['aliaccountname'] = $params['aliaccountname'];
        }
        if(!empty($params['aliimg'])){
            $data['aliimg'] = implode(',',$params['aliimg']);
        }
        if(!empty($params['bank_name'])){
            $data['bank_name'] = $params['bank_name'];
        }
        if(!empty($params['bank_address'])){
            $data['bank_address'] = $params['bank_address'];
        }
        if(!empty($params['bank_huming'])){
            $data['bank_huming'] = $params['bank_huming'];
        }
        if(!empty($params['bank_card'])){
            $data['bank_card'] = $params['bank_card'];
        }
        Db::name('users')->where('user_id',$this->user_id)->update($data);
        $result = [
            'status' => true,
            'msg' => '设置成功',
            'data' => '',
        ];
        return $result;
    }
    //设置手机号
    public function setmobile(){
        $params = input();
        //验证验证码
        $smscode = $params['smscode']??'';

        $data = [];
        $data['mobile'] = $params['tel'];
        Db::name('users')->where('user_id',$this->user_id)->update($data);
        $result = [
            'status' => true,
            'msg' => '设置成功',
            'data' => '',
        ];
        return $result;
    }
    public function paypwd(){
        $params = input();
        $userinfo = Db::name('users')->where('user_id',$this->user_id)->find();
        if($userinfo['paypwd'] && empty($params['oldpaypwd'])){
            $result = [
                'status' => 0,
                'msg' => '请输入原始密码',
                'data' => '',
            ];
            return $result;
        }
        if($userinfo['paypwd'] && encrypt($params['oldpaypwd'])!=$userinfo['paypwd']){
            $result = [
                'status' => 0,
                'msg' => '原始密码验证错误',
                'data' => '',
            ];
            return $result;
        }

        $vf = verifyPwd($params['paypwd']);
        if(!$vf['status']){
            $result = [
                'status' => 0,
                'msg' => $vf['msg'],
                'data' => '',
            ];
            return $result;
        }
        $data = [];
        $data['paypwd'] = encrypt($params['paypwd']);
        Db::name('users')->where('user_id',$this->user_id)->update($data);
        $result = [
            'status' => true,
            'msg' => '设置成功',
            'data' => '',
        ];
        return $result;
    }
    public function password(){
        $params = input();
        $userinfo = Db::name('user')->where('user_id',$this->user_id)->find();
        if($userinfo['password'] && empty($params['oldpwd'])){
            $result = [
                'status' => 0,
                'msg' => '请输入原始密码',
                'data' => '',
            ];
            return $result;
        }
        if($userinfo['password'] && encrypt($params['oldpwd'])!=$userinfo['password']){
            $result = [
                'status' => 0,
                'msg' => '原始密码验证错误',
                'data' => '',
            ];
            return $result;
        }

        $vf = verifyPwd($params['pwd']);
        if(!$vf['status']){
            $result = [
                'status' => 0,
                'msg' => $vf['msg'],
                'data' => '',
            ];
            return $result;
        }
        $data = [];
        $data['password'] = encrypt($params['pwd']);
        Db::name('users')->where('id',$this->user_id)->update($data);
        $result = [
            'status' => true,
            'msg' => '设置成功',
            'data' => '',
        ];
        return $result;
    }
    public function logout()
    {
        session(null);
        $result = [
            'status' => true,
            'msg' => '退出成功',
            'data' => '',
        ];
        return $result;
    }

    public function moneylog(){
        $page = input('pagenum');
        $limit = input('limit')?:15;
        $user_id = $this->user_id;
        $user_money = Db::name('wallet')->where('user_id',$user_id)->value('money');
        $field = '*';
        $map = [];
        $lists = Db::name('money_log')
            ->where($map)
            ->order('id desc')
            ->field($field)
            ->paginate(15,false,['page'=>$page,'list_rows'=>$limit]);
        if($lists->total()){
            foreach($lists as $k=>$v){
                $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $lists->offsetSet($k,$v);
            }
        }
        $result = [
            'status' => true,
            'msg' => '',
            'data' => $lists,
            'user_money' => $user_money
        ];
        return $result;
    }
    public function transfer(){
        $user_id = $this->user_id;
        $user_money = Db::name('wallet')->where('user_id',$user_id)->value('money');
        $result = [
            'status' => true,
            'msg' => '',
            'user_money' => $user_money
        ];
        return $result;
    }

    public function getuser(){
        $username = input('username');
        $userinfo = Db::name('users')->where('user_id',$username)->find();
        if(!$userinfo){
            $userinfo = Db::name('users')->where('username',$username)->find();
        }
        $domain = getCurUrl();
        $userinfo['head_pic'] = $userinfo['head_pic']?:$domain.'/static/img/touxiang.png';
        return ['status'=>true,'data'=>$userinfo];
    }

    public function history(){
        $pagenum = input('post.pagenum');
        if(!$pagenum) $pagenum = 1;
        $pernum = 20;
        $where = [];
        $user_id = $this->user_id;
        $where['user_id'] = $user_id;
        $datalist = Db::name('user_history')
            ->field('id,goods_id,from_unixtime(createtime)createtime,type,user_id')
            ->where($where)
            ->page($pagenum,$pernum)
            ->order('createtime desc')->select();
        if(!$datalist) $datalist = [];
        foreach($datalist as $k=>$v){
            $product = [];
            $product = Db::name('goods')->where('goods_id',$v['goods_id'])->find();
            if(!$product){
                Db::name('user_history')->where('id',$v['id'])->delete();
                unset($datalist[$k]);
            }else{
                $datalist[$k]['product'] = $product;
            }
        }
        foreach ($datalist as $k=>$v){
            $datalist[$k]['ismine'] = true;
            if($v['user_id']!=$this->user_id){
                $datalist[$k]['ismine'] = false;
            }
        }
        if(request()->isPost()){
            return ['status'=>1,'data'=>$datalist];
        }
        $count = Db::name('user_history')->where($where)->count();

        $rdata = [];
        $rdata['count'] = $count;
        $rdata['datalist'] = $datalist;
        $rdata['pernum'] = $pernum;
        return $rdata;
    }
    public function historydel(){
        $post = input('post.');
        $user_id = $this->user_id;
        if($post['proid'] =='all'){
            $rs = Db::name('user_history')->where('user_id',$user_id)->delete();
        }else{
            $rs = Db::name('user_history')->where('user_id',$user_id)->where('goods_id',$post['proid'])->where('type',$post['type'])->delete();
        }
        return['status'=>1,'msg'=>'已删除','url'=>true];
    }

    //我的团队
    public function team(){
        $user_id = input('user_id')?input('user_id/d'):0;
        if(!$user_id){
            $user_id = $this->user_id;
        }
        $date_start = 0;
        $date_end = 0;
        if(input('date_start') && input('date_end')){
            $date_start = strtotime(input('date_start'));
            $date_end = strtotime(input('date_end'));
        }
        $checkLevelid = input('checkLevelid')?input('checkLevelid/d'):0;

        $userinfo = Db::name('users')->field('user_id,nickname,head_pic,level,level1,last_login,depth')->where('user_id',$this->user_id)->find();
        $userlevel = Db::name('user_level')->where('rank',$userinfo['level'])->find();
        $userlevel1 = Db::name('user_level1')->where('rank',$userinfo['level1'])->find();

        $downdeep = input('param.st/d');
        $pernum = 20;
        $pagenum = input('post.pagenum');
        $keyword = input('post.keyword');
        $order = "id desc";

        $where2 = [];
        $query_params = [];//query查询条件
        if($keyword){
            $where2['username|nickname|mobile'] = ['like','%'.$keyword.'%'];
        }
        if($date_start && $date_end){
            $where2['reg_time'] = ['between',[$date_start,$date_end]];
        }
        if($checkLevelid){
            $where2['level'] = $checkLevelid;

        }
        if(!$pagenum) $pagenum = 1;
        if(!$downdeep) $downdeep = 1;
        if(!$user_id){
            $datalist = [];
        }else{
            if($downdeep == 1){
                $where = [];
                $where['tjr_id'] = $user_id;
                $datalist = Db::name('users')
                    ->where($where)
                    ->where($where2)
                    ->page($pagenum,$pernum)
                    ->order('reg_time desc')
                    ->select()->toArray();
            }elseif($downdeep == 2){
                $depth = $userinfo['depth']+2;
                $where = 'find_in_set('.$user_id.',tjstr) and depth='.$depth;
                $datalist = Db::name('users')
                    ->where($where)
                    ->where($where2)
                    ->page($pagenum,$pernum)
                    ->order('reg_time desc')
                    ->select()->toArray();
            }elseif($downdeep == 3){
                $depth = $userinfo['depth']+3;
                $where = 'find_in_set('.$user_id.',tjstr) and depth='.$depth;
                $datalist = Db::name('users')
                    ->where($where)
                    ->where($where2)
                    ->page($pagenum,$pernum)
                    ->order('reg_time desc')
                    ->select()->toArray();
            }elseif($downdeep == 4){
                $depth = $userinfo['depth']+0;
                $where = 'find_in_set('.$user_id.',tjstr) and depth>'.$depth;
                $datalist = Db::name('users')
                    ->where($where)
                    ->where($where2)
                    ->page($pagenum,$pernum)
                    ->order('reg_time desc')
                    ->select()->toArray();
            }
        }
        if(!$datalist) $datalist = [];
        $team_yeji_total = 0;
        $downmids = Db::name('users')->where('find_in_set('.$user_id.',tjstr)')->column('user_id');
        if($downmids){
            $yejiwhere = [];
            $yejiwhere['order_status'] = ['in','1,2,3'];
            $yejiwhere['user_id'] = ['in',$downmids];
            if($date_start && $date_end){
                $yejiwhere['pay_time'] = ['between',[$date_start,$date_end]];
            }
            $team_yeji_total = Db::name('order')->where($yejiwhere)->sum('total_shop_price');
        }

        foreach($datalist as $k=>$v){

            $commission_where = [];
            $commission_where['user_id'] = $user_id;
            $commission_where['order_userid'] = $v['user_id'];
            $commission = Db::name('bonus_detail')
                ->where($commission_where)
                ->sum('bonus');

            $datalist[$k]['commission'] = $commission;
            $downcount_where = [];
            $downcount_where['tjr_id'] = $v['user_id'];
            if($date_start && $date_end){
                $downcount_where['reg_time'] = ['between',[$date_start,$date_end]];
            }
            $datalist[$k]['downcount'] = Db::name('users')->where($downcount_where)->count();

            $level = Db::name('user_level')->where('rank',$v['level'])->find();
            $datalist[$k]['nickname'] = $v['nickname']?:$v['username'];
            $datalist[$k]['levelname'] = $level['level_name'];
            $datalist[$k]['last_login'] = $v['last_login']?date('Y-m-d H:i:s',$v['last_login']):'';
            $datalist[$k]['reg_time'] = $v['reg_time']?date('Y-m-d H:i:s',$v['reg_time']):'';
            if($userlevel['team_showtel'] == 0){
                $datalist[$k]['tel'] = '';
            }
            $domain_url = getCurUrl();
            $datalist[$k]['head_pic'] = $v['head_pic']?$v['head_pic']:$domain_url.'/static/img/touxiang.png';;


            $downmids = Db::name('users')->where('find_in_set('.$v['user_id'].',tjstr)')->column('user_id');
            //下级人数
            $team_down_total = count($downmids);
            $datalist[$k]['team_down_total'] = $team_down_total;

            $yejiwhere = [];
            $yejiwhere['order_status'] = ['in','1,2,3'];
            $yejiwhere['user_id'] = ['in',$downmids];
            $teamyeji = Db::name('order')->where($yejiwhere)->sum('total_shop_price');
            //个人业绩
            $yejiwhere = [];
            $yejiwhere['order_status'] = ['in','1,2,3'];
            $yejiwhere['user_id'] = $v['user_id'];
            $self_yeji = Db::name('order')->where($yejiwhere)->sum('total_shop_price');
            //业绩包含自身
            $datalist[$k]['teamyeji'] = $teamyeji;
            $datalist[$k]['selfyeji'] = $self_yeji;
        }
        $month_yeji_show = false;

        $userinfo['month_yeji_show'] = $month_yeji_show;
        //团队业绩
        $downmids = Db::name('users')->where('find_in_set('.$user_id.',tjstr)')->column('user_id');
        $userinfo['team_down_total'] = count($downmids);

        $userinfo['team_yeji_total'] =$team_yeji_total;
        //距离下个等级
        $userinfo['next_ordermoney_show'] = false;
        if($pagenum == 1){
            //我的团队
            //一级人数
            $team_where = [];
            $team_where['tjr_id'] = $user_id;
            if($date_start && $date_end){
                $team_where['reg_time'] = ['between',[$date_start,$date_end]];
            }
            $userinfo['myteamCount1'] = Db::name('users')->where($team_where)->count();
            //二级人数
            $depth = $userinfo['depth']+2;
            $team_where2 = 'find_in_set('.$user_id.',tjstr) and depth='.$depth;
            $myteamCount2 = Db::name('users')->where($team_where2)->count();
            $userinfo['myteamCount2'] = $myteamCount2;
            //三级人数
            $depth = $userinfo['depth']+3;
            $team_where3 = 'find_in_set('.$user_id.',tjstr) and depth='.$depth;
            $myteamCount3 = Db::name('users')->where($team_where3)->count();
            $userinfo['myteamCount3'] = $myteamCount3
            ;
            $userinfo['myteamCount'] = $userinfo['myteamCount1'] + $userinfo['myteamCount2'];
            //三级以后的团队人数
            $depth = $userinfo['depth']+0;
            $team_where4 = 'find_in_set('.$user_id.',tjstr) and depth>'.$depth;
            $myteamCount4 = Db::name('users')->where($team_where4)->count();
            $userinfo['myteamCount4'] = $myteamCount4;

        }
        //小部门业绩[只统计确认收货且减掉退款的]
        $userinfo['miniyeji_show'] = false;
        $downmids = Db::name('users')->where('find_in_set('.$user_id.',tjstr)')->column('user_id');
        $yejiwhere = [];
        $yejiwhere[] = ['order_status','in','1,2,3'];
        $yejiwhere[] = ['user_id','in',$downmids];
        $month_teamyeji = Db::name('order')->where('user_id','in',$downmids)->where($yejiwhere)->sum('total_shop_price');

        $down_user_ids = Db::name('users')->where('find_in_set('.$user_id.',tjstr)')->column('user_id');
        //上月1号
        $modtime = getCurTime();
        $s_time = mktime(0, 0, 0, date('m', $modtime) - 1, 1, date('Y', $modtime));
        //本月1号
        $e_time = strtotime(date('Y-m-1',$modtime));
        $last_month_yeji =Db::name('order')
            ->where('order_status','in',[1,2,3])
            ->where('pay_time','between time',[$s_time,$e_time])
            ->where('user_id','in',$down_user_ids)
            ->sum('total_shop_price');
        $month_yeji = Db::name('order')
            ->where('order_status','in',[1,2,3])
            ->where('pay_time','>',$e_time)
            ->where('user_id','in',$down_user_ids)
            ->sum('total_shop_price');
        $userinfo['last_month_yeji'] = $last_month_yeji;
        $userinfo['month_yeji'] = $month_yeji;

        $custom = ['_t'=>time()];//默认


        $rdata = [];
        $rdata['datalist'] = $datalist;
        $rdata['userinfo'] = $userinfo;
        $rdata['custom'] = $custom;

        $rdata['userlevel'] = $userlevel;

        $levelList = Db::name('user_level')->where('1=1')->column('level_name','rank');
        $rdata['levelList'] = $levelList;
        $rdata['st'] = $downdeep;
        $rdata['team_auth'] = 1;
        //所有级别
        $all_level = Db::name('user_level')->where('1=1')->select();
        $rdata['all_level'] = $all_level;
        return $rdata;
    }

    //设置微信小程序openid
    public function setwxopenid(){
        if(!$this->user) {
            return ['status'=>0,'msg'=>'未登录'];
        }
        $plate = input('plate')?:'wx';
        $post = input('post.');
        $jscode = input('post.code');
        $set = logic('SocialLogin')->appinfo($plate);
        if(!$set || !$set['appid'] || !$set['appsecret']){
            return ['status'=>0,'msg'=>'未配置平台信息，请联系管理员配置'];
        }
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$set['appid'].'&secret='.$set['appsecret'].'&js_code='.$jscode.'&grant_type=authorization_code';
        $rs = request_get($url);
        $rs = json_decode($rs,true);
        if($rs['errcode']>0){
            $msg = $rs['errmsg']?:'';
            return ['status'=>0,'msg'=>$rs['errcode'].$msg];
        }
        $openid = $rs['openid'];
        $unionid = $rs['unionid'];
        if($openid){
            $updata = [];
            $updata['wxopenid'] = $openid;
            if($unionid){
                $updata['unionid'] = $unionid;
            }
            Db::name('users')->where('user_id',$this->user_id)->update($updata);
        }
        return ['status'=>1,'msg'=>'更新成功'];
    }
    //设置支付宝小程序openid
    public function setalipayopenid(){
        if(!$this->member) return json(['status'=>0,'msg'=>'未登录']);
        $post = input('post.');
        $jscode = input('post.code');
        $plate = input('plate')?:'ali';
        $set = logic('SocialLogin')->appinfo($plate);
        if(!$set || !$set['appid'] || !$set['appsecret']){
            return ['status'=>0,'msg'=>'未配置平台信息，请联系管理员配置'];
        }
        require_once(ROOT_PATH.'/extend/aop/AopClient.php');
        require_once(ROOT_PATH.'/extend/aop/request/AlipaySystemOauthTokenRequest.php');

        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $set['appid'];
        $aop->rsaPrivateKey = $set['appsecret'];
        $aop->alipayrsaPublicKey= $set['publickey'];
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'utf-8';
        $aop->format='json';
        $request = new \AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($jscode);
        $result = $aop->execute ($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;

        if(!empty($resultCode)&&$resultCode != 10000){
            return json(['status'=>0,'msg'=>$result->$responseNode->sub_msg]);
        } else {
            $openid = $result->$responseNode->user_id;
            $openid_new = $result->$responseNode->open_id;
            if(!$openid && !$openid_new){
                return json(['status'=>0,'msg'=>'获取授权信息失败']);
            }
            $updata = [];
            if($openid)$updata['alipayopenid'] = $openid;
            if($openid_new)$updata['alipayopenid_new'] = $openid_new;
            Db::name('users')->where('user_id',$this->user_id)->update($updata);
            return ['status'=>1,'msg'=>'更新成功','openid'=>$openid?$openid:$openid_new];
        }
    }

    //找回密码
    public function getpaypwd(){
        if(request()->isPost()){
            $user = Db::name('users')->where('user_id',$this->user_id)->find();
            $username = $user['username'];
            $tel = $user['mobile'];
            $pwd = input('param.pwd');
            $smscode = input('param.smscode');
            $map = [];
            $map['username'] = $username;
            $map['mobile'] = $tel;
            $map['status'] = 0;
            $exit_smscode = Db::name("send_sms")->where($map)->order('id desc')->find();
            if(!$exit_smscode || $exit_smscode['codes']!=$smscode){
                 return ['status'=>0,'msg'=>'短信验证码错误'];
            }

            Db::name('users')->where('user_id',$user['user_id'])->update(['paypwd'=>encrypt($pwd)]);
            Db::name("send_sms")->where('id',$exit_smscode['id'])->update(['status'=>1]);
            return ['status'=>1,'msg'=>'重置成功'];
        }else{
            $rdata = [];
            $rdata['status'] = 1;


            return $rdata;
        }

    }
    public function sendUsersms(){
        $exit = Db::name('users')->where('user_id',$this->user_id)->find();
        if(!$exit){
            $result = [
                'status' => 0,
                'msg' => '账号不存在！'
            ];
            return $result;
        }
        $res = logic('SmsLogic')->sendSms('find_sms',$exit['mobile'],$exit['username']);
        writeLog(json_encode($res,JSON_UNESCAPED_UNICODE),'sms');
        if(!$res['status']){
            return $res;
        }
        $result = [
            'status' => 1,
            'msg' => '发送成功！'
        ];
        return $result;
    }

}
